//
//  VerifyPhoneVC.m
//  BigJoy
//
//  Created by Rowling on 2017/7/3.
//  Copyright © 2017年 Rowling. All rights reserved.
//

#import "VerifyPhoneVC.h"
#import "SetPasswordVC.h"
#import "JKCountDownButton.h"
#import "VerifyViewModel.h"
#import "UITextField+Placeholder.h"
@interface VerifyPhoneVC ()

@property (weak, nonatomic) IBOutlet UITextField *phoneTextFeild;
@property (weak, nonatomic) IBOutlet UITextField *codeTextField;
@property (weak, nonatomic) IBOutlet UIView *findBackView;
@property (weak, nonatomic) IBOutlet UITextField *paswodTextField;
@property (weak, nonatomic) IBOutlet UIImageView *IconImageView;
@property (weak, nonatomic) IBOutlet UIButton *nextButton;

@property (nonatomic,strong) VerifyViewModel *viewModel;
@end

@implementation VerifyPhoneVC

-(VerifyViewModel *)viewModel {
    if (_viewModel == nil) {
        _viewModel = [[VerifyViewModel alloc]init];
        [_viewModel bindView:self.view];
    }
    return _viewModel;
}



- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"验证手机";
    @weakify(self);
    [[[self.paswodTextField rac_textSignal] map:^id _Nullable(NSString * _Nullable value) {
        if (value.length >= 20) {
            return [value substringWithRange:NSMakeRange(0, 20)];
        }
        return value;
    }] subscribeNext:^(id  _Nullable x) {
        @strongify(self);
        NSLog(@"%@",x);
        self.paswodTextField.text = x;
    }];
    if (self.passType == FindBack) {
        // 找回密码
        [self.nextButton setTitle:@"确认找回" forState:UIControlStateNormal];
        self.findBackView.hidden = NO;
    }else if(self.passType == BindPhon) {
        //绑定手机
        self.findBackView.hidden = NO;
        self.IconImageView.image = [UIImage imageNamed:@"fenxiang_hui"];
        self.paswodTextField.placeholder = @"请填写邀请码(选填)";
        self.paswodTextField.placeholderColor = XMColor(220, 220, 220);
        [self.nextButton setTitle:@"完成绑定" forState:UIControlStateNormal];
        UIButton * btn = [[UIButton alloc]init];
        [btn setTitle:@"跳过" forState:UIControlStateNormal];
        [btn sizeToFit];
        [btn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
        self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc]initWithCustomView:btn];
        @weakify(self);
        [[btn rac_signalForControlEvents:UIControlEventTouchUpInside] subscribeNext:^(__kindof UIControl * _Nullable x) {
            @strongify(self);
            [self.navigationController dismissViewControllerAnimated:YES completion:nil];
        }];
    }
}

- (IBAction)getCodeButtonClick:(JKCountDownButton *)sender {
    if (![XMSuperHelper isMobileNumber:self.phoneTextFeild.text]) {
        [Apputil showError:@"手机号码格式不正确"];
        return;
    }
    
    
    NSDictionary * dic = @{
                           @"phone" : self.phoneTextFeild.text,
                           @"cmsType" : @(self.passType)
                           };
    [[self.viewModel.getMeassgeCommand execute:dic] subscribeNext:^(id  _Nullable x) {
        if ([x[@"code"] longLongValue] < 0) {
             [Apputil showError:x[@"msg"]];
        } else {
            [sender startCountDownWithSecond:60];
            sender.enabled = NO;
        }
        NSLog(@"%@",x);
    }];
    [sender countDownChanging:^NSString *(JKCountDownButton *countDownButton,NSUInteger second) {
        NSString *title = [NSString stringWithFormat:@"剩余%zd秒",second];
        return title;
    }];
    [sender countDownFinished:^NSString *(JKCountDownButton *countDownButton, NSUInteger second) {
        countDownButton.enabled = YES;
        return @"重新获取";
    }];
}

- (IBAction)nextButtonClick:(id)sender {
    if (![XMSuperHelper isMobileNumber:self.phoneTextFeild.text]) {
        [Apputil showError:@"手机号码格式不正确"];
        return;
    }
    if ([self.codeTextField.text isEqualToString:@""]) {
        [Apputil showError:@"请输入验证码"];
        return;
    }
    if (self.passType == FindBack) {
        [self findbcakMethod];
    }else if(self.passType == normal) {
        [self registerMethod];
    } else if(self.passType == BindPhon) {
        [self bindPhoneMethod];
    }
}
// 绑定手机
-(void)bindPhoneMethod {
    NSDictionary * dic = @{
                           @"uid" : [UserTool getTheUserModel].ID,
                           @"phone" : self.phoneTextFeild.text,
                           @"cmsType" : @(self.passType),
                           @"code" : self.codeTextField.text,
                           @"be_invited" : self.paswodTextField.text
                           };
    [WYNetTool GET_Urlstring:BindPhoneUrl parameters:dic success:^(id responseObject) {
        if (SuccessCode) {
            [Apputil showError:@"绑定成功"];
            [self.navigationController dismissViewControllerAnimated:YES completion:nil];
        }else {
            [Apputil showError:responseObject[@"msg"]];
        }
    } fail:^(id error) {
        
    }];
}
//找回密码
-(void)findbcakMethod {
    if (self.paswodTextField.text.length < 6) {
        [Apputil showError:@"密码不能小于6位"];
        return;
    }
    NSDictionary * dic = @{
                           @"phone" : self.phoneTextFeild.text,
                           @"cmsType" : @2,
                           @"code" : self.codeTextField.text,
                           @"pwds" : self.paswodTextField.text
                           };
    [WYNetTool GET_Urlstring:FindBackUrl parameters:dic success:^(id responseObject) {
        if([responseObject[@"code"] longLongValue] == 1) {
            [Apputil showMessage:@"找回成功" afterDelay:1];
            [self.navigationController popViewControllerAnimated:YES];
        }
    } fail:^(id error) {
        
    }];
}
// 注册
-(void)registerMethod {
    [Apputil showMessage:@"验证中.."];
    NSDictionary * dic = @{
                           @"phone" : self.phoneTextFeild.text,
                           @"cmsType" : @1,
                           @"code" : self.codeTextField.text
                           };
    @weakify(self);
    [[self.viewModel.verifyCoedCommand execute:dic] subscribeNext:^(id  _Nullable x) {
        @strongify(self);
        if ([x[@"code"] longLongValue] < 0) {
            [Apputil showError:x[@"msg"]];
        }else {
            SetPasswordVC * vc = [SetPasswordVC new];
            vc.phoneStr = self.phoneTextFeild.text;
            [self.navigationController pushViewController:vc animated:YES];
            [Apputil dismiss];
        }
    }];
}


@end
